﻿
namespace Oddo.Concept.Crm.Common.Extensions
{
    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    using Oddo.Concept.Crm.Common.Interfaces;

    /// <summary>
    /// Class d'extensions
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// Declenche l'evenement PropertyChanged
        /// </summary>
        public static void RaisePropertyChanged<T>(this INotifyPropertyChangedEx notifier,   Expression<Func<T>> propertyExpression)
        {
            if (!notifier.ShouldNotifyPropertyChanged) return;

            if (propertyExpression == null)
                throw new ArgumentNullException("propertyExpression");
            var memberExpression = propertyExpression.Body as MemberExpression;
            if (memberExpression == null)
                throw new ArgumentException("propertyExpression");
            var propertyInfo = memberExpression.Member as PropertyInfo;
            if (propertyInfo == (PropertyInfo)null)
                throw new ArgumentException("propertyExpression");
            if (propertyInfo.GetGetMethod(true).IsStatic)
                throw new ArgumentException("propertyExpression");

            var propertyName = memberExpression.Member.Name;
            notifier.NotifyPropertyChanged(propertyName);
        }
    }
}